home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / amiga / gui / x / xfig.lha / src / x11 / f_xbitmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-26  |  5.5 KB  |  166 lines

  1. /*
  2.  * FIG : Facility for Interactive Generation of figures
  3.  * Copyright (c) 1992 by Brian V. Smith
  4.  *
  5.  * "Permission to use, copy, modify, distribute, and sell this software and its
  6.  * documentation for any purpose is hereby granted without fee, provided that
  7.  * the above copyright notice appear in all copies and that both the copyright
  8.  * notice and this permission notice appear in supporting documentation. 
  9.  * No representations are made about the suitability of this software for 
  10.  * any purpose.  It is provided "as is" without express or implied warranty."
  11.  */
  12.  
  13. #include "fig.h"
  14. #include "resources.h"
  15. #include "mode.h"
  16. #include "object.h"
  17. #include "paintop.h"
  18. #include "w_setup.h"
  19. #include "w_drawprim.h"
  20.  
  21. static int    create_n_write_bitmap();
  22.  
  23. int
  24. write_bitmap(file_name)
  25.     char       *file_name;
  26. {
  27.     if (!ok_to_write(file_name, "EXPORT"))
  28.     return (1);
  29.  
  30.     return (create_n_write_bitmap(file_name));    /* write the bitmap file */
  31. }
  32.  
  33. static Boolean    havegcs = False;
  34. static GC    sav_fill_gc[NUMFILLPATS];
  35. static GC    sav_un_fill_gc[NUMFILLPATS];
  36. static unsigned long save_fg_color;
  37. static unsigned long save_bg_color;
  38.  
  39. static int
  40. create_n_write_bitmap(filename)
  41.     char       *filename;
  42. {
  43.     int            xmin, ymin, xmax, ymax;
  44.     int            width, height;
  45.     Window        sav_canvas;
  46.     int            sav_objmask;
  47.     Pixmap        largepm, bitmap;
  48.     extern F_compound objects;
  49.     int            i;
  50.     GC            xgc, gc_bitmap;
  51.  
  52.     /* this may take a while */
  53.     set_temp_cursor(wait_cursor);
  54.     put_msg("Capturing canvas image...");
  55.     app_flush();
  56.  
  57.     /* Assume that there is at least one object */
  58.     compound_bound(&objects, &xmin, &ymin, &xmax, &ymax);
  59.  
  60.     if (appres.DEBUG) {
  61.     elastic_box(xmin, ymin, xmax, ymax);
  62.     }
  63.     /* provide a small margin */
  64.     if ((xmin -= 10) < 0)
  65.     xmin = 0;
  66.     if ((ymin -= 10) < 0)
  67.     ymin = 0;
  68.     if ((xmax += 10) > CANVAS_WD)
  69.     xmax = CANVAS_WD;
  70.     if ((ymax += 10) > CANVAS_HT)
  71.     ymax = CANVAS_HT;
  72.  
  73.     width = xmax - xmin + 1;
  74.     height = ymax - ymin + 1;
  75.  
  76.     /* choose foreground/background colors as 1 and 0 respectively */
  77.     /* that way we can just copy the lowest plane to make the bitmap */
  78.  
  79.     XSetPlaneMask(tool_d, gccache[PAINT], (unsigned long) 1);
  80.     XSetForeground(tool_d, gccache[PAINT], (unsigned long) 1);
  81.     XSetBackground(tool_d, gccache[PAINT], (unsigned long) 0);
  82.     XSetPlaneMask(tool_d, gccache[ERASE], (unsigned long) 1);
  83.     XSetForeground(tool_d, gccache[ERASE], (unsigned long) 0);
  84.     XSetBackground(tool_d, gccache[ERASE], (unsigned long) 0);
  85.     save_fg_color = x_color(cur_color);    /* save current colors */
  86.     save_bg_color = x_bg_color.pixel;
  87.     x_fg_color.pixel = 1;        /* set fore=1, back=0 */
  88.     x_bg_color.pixel = 0;
  89.     writing_bitmap = True;        /* so the colors don't change */
  90.     if (!havegcs) {
  91.     havegcs = True;
  92.     for (i = 0; i < NUMFILLPATS; i++) {    /* save current fill gc's */
  93.         sav_fill_gc[i] = fill_gc[i];
  94.         sav_un_fill_gc[i] = un_fill_gc[i];
  95.     }
  96.     init_fill_gc();        /* make some with 0/1 for colors */
  97.     } else
  98.     for (i = 0; i < NUMFILLPATS; i++) {
  99.         xgc = sav_fill_gc[i];    /* swap our gc's with orig */
  100.         sav_fill_gc[i] = fill_gc[i];
  101.         fill_gc[i] = xgc;
  102.         xgc = sav_un_fill_gc[i];
  103.         sav_un_fill_gc[i] = un_fill_gc[i];
  104.         un_fill_gc[i] = xgc;
  105.     }
  106.  
  107.     /* create pixmap from (0,0) to (xmax,ymax) */
  108.     largepm = XCreatePixmap(tool_d, canvas_win, xmax + 1, ymax + 1,
  109.                 DefaultDepthOfScreen(tool_s));
  110.     /* clear it */
  111.     XFillRectangle(tool_d, largepm, gccache[ERASE], 0, 0, xmax+1, ymax+1);
  112.     sav_canvas = canvas_win;    /* save current canvas window id */
  113.     canvas_win = largepm;    /* make the canvas our pixmap */
  114.     sav_objmask = cur_objmask;    /* save the point marker */
  115.     cur_objmask = M_NONE;
  116.     redisplay_objects(&objects);/* draw the figure into the pixmap */
  117.     put_msg("Writing bitmap file...");
  118.     app_flush();
  119.  
  120.     x_fg_color.pixel = save_fg_color;    /* put colors back to normal */
  121.     x_bg_color.pixel = save_bg_color;
  122.     XSetPlaneMask(tool_d, gccache[PAINT], (unsigned long) AllPlanes);
  123.     XSetForeground(tool_d, gccache[PAINT], x_fg_color.pixel);
  124.     XSetBackground(tool_d, gccache[PAINT], x_bg_color.pixel);
  125.     XSetPlaneMask(tool_d, gccache[ERASE], (unsigned long) AllPlanes);
  126.     XSetForeground(tool_d, gccache[ERASE], x_bg_color.pixel);
  127.     XSetBackground(tool_d, gccache[ERASE], x_bg_color.pixel);
  128.  
  129.     writing_bitmap = False;
  130.     canvas_win = sav_canvas;    /* go back to the real canvas */
  131.     cur_objmask = sav_objmask;    /* restore point marker */
  132.     bitmap = XCreatePixmap(tool_d, canvas_win, width, height, 1);
  133.     gc_bitmap = XCreateGC(tool_d, bitmap, 0L, NULL);
  134.     /* set the foreground back to 1 */
  135.     XSetForeground(tool_d, gc_bitmap, 1);
  136.     /* and the background back to 0 */
  137.     XSetBackground(tool_d, gc_bitmap, 0);
  138.     /* now copy one plane of the pixmap to a bitmap of the correct size */
  139.     XCopyPlane(tool_d, largepm, bitmap, gc_bitmap,
  140.            xmin, ymin, width, height, 0, 0, 1);
  141.     for (i = 0; i < NUMFILLPATS; i++) { /* swap back the fill gc's */
  142.     xgc = sav_fill_gc[i];
  143.     sav_fill_gc[i] = fill_gc[i];
  144.     fill_gc[i] = xgc;
  145.     xgc = sav_un_fill_gc[i];
  146.     sav_un_fill_gc[i] = un_fill_gc[i];
  147.     un_fill_gc[i] = xgc;
  148.     }
  149.     if (XWriteBitmapFile(tool_d, filename, bitmap, width, height, -1, -1)
  150.     != BitmapSuccess) {
  151.     put_msg("Couldn't write bitmap file");
  152.     XFreePixmap(tool_d, largepm);
  153.     XFreePixmap(tool_d, bitmap);
  154.     /* all done */
  155.     reset_cursor();
  156.     return (1);
  157.     } else {
  158.     put_msg("Bitmap written to \"%s\"", filename);
  159.     XFreePixmap(tool_d, largepm);
  160.     XFreePixmap(tool_d, bitmap);
  161.     /* all done */
  162.     reset_cursor();
  163.     return (0);
  164.     }
  165. }
  166.